package service

import (
	"ksd-social-api/commons/base/service"
	"ksd-social-api/modules/note/mapper"
	"ksd-social-api/modules/note/model"
	"ksd-social-api/modules/note/model/context"
	"ksd-social-api/modules/note/model/vo"
	"ksd-social-api/utils"
	"strconv"
	"time"
)

type NoteLessonService struct {
	service.BaseService
}

var noteLessonMapper = mapper.NoteLessonMapper{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询章信息
 */
func (service *NoteLessonService) FindLessonsStatus(noteId uint64, chapterId uint64, systemId uint64) ([]*vo.NoteLessonVo, error) {
	var noteLessonVos []*vo.NoteLessonVo
	lessonList, _ := noteLessonMapper.FindLessonsStatus(noteId, chapterId, systemId)
	for _, lesson := range lessonList {
		var noteLessonVo vo.NoteLessonVo
		utils.CopyProperties(&noteLessonVo, lesson)
		noteLessonVo.Id = strconv.FormatUint(lesson.Id, 10)
		noteLessonVo.ChapterId = strconv.FormatUint(lesson.ChapterId, 10)
		noteLessonVo.NoteId = strconv.FormatUint(lesson.NoteId, 10)
		noteLessonVos = append(noteLessonVos, &noteLessonVo)
	}
	return noteLessonVos, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询章信息
 */
func (service *NoteLessonService) FindLessons(noteId uint64, chapterId uint64, systemId uint64) ([]*vo.NoteLessonVo, error) {
	var noteLessonVos []*vo.NoteLessonVo
	lessonList, _ := noteLessonMapper.FindLessons(noteId, chapterId, systemId)
	for _, lesson := range lessonList {
		var noteLessonVo vo.NoteLessonVo
		utils.CopyProperties(&noteLessonVo, lesson)
		noteLessonVo.Id = strconv.FormatUint(lesson.Id, 10)
		noteLessonVo.NoteId = strconv.FormatUint(lesson.NoteId, 10)
		noteLessonVo.ChapterId = strconv.FormatUint(lesson.ChapterId, 10)
		noteLessonVos = append(noteLessonVos, &noteLessonVo)
	}
	return noteLessonVos, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询章信息
 */
func (service *NoteLessonService) GetById(id uint64, systemId uint64) (v *vo.NoteLessonVo, err error) {
	lesson, _ := noteLessonMapper.GetById(id, systemId)
	noteLessonVo := vo.NoteLessonVo{}
	utils.CopyProperties(&noteLessonVo, lesson)
	noteLessonVo.Id = strconv.FormatUint(lesson.Id, 10)
	noteLessonVo.NoteId = strconv.FormatUint(lesson.NoteId, 10)
	noteLessonVo.ChapterId = strconv.FormatUint(lesson.ChapterId, 10)
	return &noteLessonVo, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 保存章章信息
 */
func (service *NoteLessonService) SaveLesson(ctx context.NoteLessonContext) (lessonVo *vo.NoteLessonVo, err error) {

	noteLesson := model.NoteLesson{}
	utils.CopyProperties(&noteLesson, ctx)
	NoteId, _ := strconv.ParseUint(ctx.NoteId, 10, 64)
	noteLesson.NoteId = NoteId
	ChapterId, _ := strconv.ParseUint(ctx.ChapterId, 10, 64)
	noteLesson.ChapterId = ChapterId
	noteLesson.FreeFlag = 1
	lesson, _ := noteLessonMapper.SaveLesson(&noteLesson)
	noteLessonVo := vo.NoteLessonVo{}
	utils.CopyProperties(&noteLessonVo, lesson)
	noteLessonVo.Id = strconv.FormatUint(lesson.Id, 10)
	noteLessonVo.ChapterId = strconv.FormatUint(lesson.ChapterId, 10)
	noteLessonVo.NoteId = strconv.FormatUint(lesson.NoteId, 10)
	return &noteLessonVo, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 复制章章信息
 */
func (service *NoteLessonService) CopyLesson(id uint64, systemId uint64) (lessonVo *vo.NoteLessonVo, err error) {
	newLesson, _ := noteLessonMapper.GetById(id, systemId)
	newLesson.Id = 0
	newLesson.Status = 1
	newLesson.Title = newLesson.Title + "[副本]"
	newLesson.CreateTime = time.Now()
	lesson, err := noteLessonMapper.SaveLesson(newLesson)
	noteLessonVo := vo.NoteLessonVo{}
	utils.CopyProperties(&noteLessonVo, lesson)
	noteLessonVo.Id = strconv.FormatUint(lesson.Id, 10)
	noteLessonVo.ChapterId = strconv.FormatUint(lesson.ChapterId, 10)
	noteLessonVo.NoteId = strconv.FormatUint(lesson.NoteId, 10)
	return &noteLessonVo, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改章信息
 */
func (service *NoteLessonService) UpdateById(ctx context.NoteLessonStatusContext) (flag bool, err error) {
	ID, _ := strconv.ParseUint(ctx.Id, 10, 64)
	return noteLessonMapper.UpdateById(ctx.Field, ctx.Value, ID)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改章信息
 */
func (service *NoteLessonService) RemoveByID(ctx context.NoteLessonRemoveContext) (flag bool, err error) {
	NoteId, _ := strconv.ParseUint(ctx.NoteId, 10, 64)
	ID, _ := strconv.ParseUint(ctx.Id, 10, 64)
	return noteLessonMapper.RemoveById(ID, NoteId, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改章信息
 */
func (service *NoteLessonService) UpdateByIdContent(ctx context.NoteLessonContentContext) (flag bool, err error) {
	NoteId, _ := strconv.ParseUint(ctx.NoteId, 10, 64)
	ID, _ := strconv.ParseUint(ctx.Id, 10, 64)
	return noteLessonMapper.UpdateByIdContent(ctx.Mdcontent, ctx.Htmlcontent, ctx.SystemId, NoteId, ID)
}
